package com.example.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dao.PostDAO;
import com.example.model.Post;
import com.example.model.User;

@WebServlet("/posts/*")
public class PostServlet extends HttpServlet {
    private PostDAO postDAO = new PostDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 检查用户是否登录
        if (req.getSession().getAttribute("user") == null) {
            resp.sendRedirect(req.getContextPath() + "/auth/login");
            return;
        }

        String pathInfo = req.getPathInfo();
        try {
            if (pathInfo == null || pathInfo.equals("/")) {
                req.setAttribute("posts", postDAO.getAllPosts());
                req.getRequestDispatcher("/WEB-INF/views/list.jsp").forward(req, resp);
            } else if (pathInfo.equals("/new")) {
                req.getRequestDispatcher("/WEB-INF/views/form.jsp").forward(req, resp);
            } else if (pathInfo.equals("/edit")) {
                int id = Integer.parseInt(req.getParameter("id"));
                req.setAttribute("post", postDAO.getPost(id));
                req.getRequestDispatcher("/WEB-INF/views/form.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 检查用户是否登录
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            resp.sendRedirect(req.getContextPath() + "/auth/login");
            return;
        }

        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        try {
            if ("create".equals(action)) {
                Post post = new Post();
                post.setTitle(req.getParameter("title"));
                post.setContent(req.getParameter("content"));
                post.setUserId(user.getId());
                postDAO.addPost(post);
            } else if ("update".equals(action)) {
                Post post = new Post();
                post.setId(Integer.parseInt(req.getParameter("id")));
                post.setTitle(req.getParameter("title"));
                post.setContent(req.getParameter("content"));
                post.setUserId(user.getId());
                postDAO.updatePost(post);
            } else if ("delete".equals(action)) {
                int id = Integer.parseInt(req.getParameter("id"));
                postDAO.deletePost(id, user.getId());
            }
            resp.sendRedirect(req.getContextPath() + "/posts");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e);
        }
    }
} 